<?php

namespace common\models;

use Yii;
use yii\data\Pagination;
/**
 * This is the model class for table "{{%nav}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $desc
 *
 * @property Nav[] $navs
 */
class Nav extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%nav}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['title', 'desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '分类id',
            'title' => '导航名称',
            'desc' => '导航描述',
        ];
    }

    /**
     * 获取所有的导航信息
     * @param $map
     * @return array|\yii\db\ActiveRecord[]
     */
    static public function getAllNav($map,$format = false)
    {
        $data = [];
        $query = static::find()->where($map)->asArray()->all();
        if ($format && !empty($query)){
            array_walk($query , function (&$v) use (&$data){
                $data['options'] = ['class' => 'nav navbar-nav navbar-right'];
                $v['options'] = [];
                $v['url'] = [$v['url']];
                $v['label'] = $v['title'];
                $data['items'][] = $v;
            });
        } else {
            $data = $query;
        }
        return $data;
    }

    /**
     * 查询一条广告信息
     * @param $params
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getFindOne($params)
    {
        return static::find()->where($params)->one();
    }
}
